/*
 * CDLibrary.java
 *
 * Created on July 28, 2003, 12:19 PM
 */

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;

/**
 *
 * @author  Hariprasad
 */
public class CDLibrary extends javax.swing.JFrame {
    
    static Vector Cds,CdColumnNames;
    static CDLibrary mainframe;
    static Properties properties;
    
    /** Creates new form CDLibrary */
    public CDLibrary() {        
        Toolkit tk=this.getToolkit();
        
        VectorInitialisation();
        initComponents();
        CDTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        try {
        screensize=tk.getScreenSize();
        } catch(HeadlessException e) { }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        CDsPopup = new javax.swing.JPopupMenu();
        UnselectCD = new javax.swing.JMenuItem();
        RemoveCD = new javax.swing.JMenuItem();
        AboutDialog = new javax.swing.JDialog();
        AboutLabel = new javax.swing.JLabel();
        flagpanel = new javax.swing.JPanel();
        IndiaFlag = new javax.swing.JLabel();
        SearchDialog = new javax.swing.JDialog();
        SearchDialog.setSize(350,225);
        slabel = new javax.swing.JLabel();
        stextfield = new javax.swing.JTextField();
        slabeltype = new javax.swing.JLabel();
        cdtype = new javax.swing.JRadioButton();
        filetype = new javax.swing.JRadioButton();
        searchbutton = new javax.swing.JButton();
        closebutton = new javax.swing.JButton();
        SortDialog = new javax.swing.JDialog();
        SortDialog.setSize(280,250);
        SortButton = new javax.swing.JButton();
        CloseButton = new javax.swing.JButton();
        SortByPanel = new javax.swing.JPanel();
        SortByLabel = new javax.swing.JLabel();
        LabelRadio = new javax.swing.JRadioButton();
        InfoRadio = new javax.swing.JRadioButton();
        LenderRadio = new javax.swing.JRadioButton();
        OrderPanel = new javax.swing.JPanel();
        OrderLabel = new javax.swing.JLabel();
        AscendingCheck = new javax.swing.JCheckBox();
        DescendingCheck = new javax.swing.JCheckBox();
        SearchDialogButtonGroup = new javax.swing.ButtonGroup();
        SortDialogButtonGroup = new javax.swing.ButtonGroup();
        SortOrderButtonGroup = new javax.swing.ButtonGroup();
        themesbuttongroup = new javax.swing.ButtonGroup();
        Label = new javax.swing.JLabel();
        SplashscreenLabel = new javax.swing.JLabel();
        tabbedpane = new javax.swing.JTabbedPane();
        CDScrollPane = new javax.swing.JScrollPane();
        CDTable = new JTable(Cds,CdColumnNames);
        menubar = new javax.swing.JMenuBar();
        tmenu_file = new javax.swing.JMenu();
        menu_new = new javax.swing.JMenuItem();
        menu_update = new javax.swing.JMenuItem();
        menu_exit = new javax.swing.JMenuItem();
        tmenu_edit = new javax.swing.JMenu();
        menu_refresh = new javax.swing.JMenuItem();
        menu_count = new javax.swing.JMenuItem();
        separator = new javax.swing.JSeparator();
        menu_sort = new javax.swing.JMenuItem();
        menu_search = new javax.swing.JMenuItem();
        tmenu_view = new javax.swing.JMenu();
        cmenu_viewlogo = new javax.swing.JCheckBoxMenuItem();
        viewSeparator1 = new javax.swing.JSeparator();
        cmenu_OnlyLended = new javax.swing.JCheckBoxMenuItem();
        tmenu_themes = new javax.swing.JMenu();
        cmenu_metal = new javax.swing.JCheckBoxMenuItem();
        cmenu_motif = new javax.swing.JCheckBoxMenuItem();
        cmenu_system = new javax.swing.JCheckBoxMenuItem();
        tmenu_about = new javax.swing.JMenu();
        menu_about = new javax.swing.JMenuItem();

        CDsPopup.setFont(new java.awt.Font("Dialog", 0, 12));
        UnselectCD.setFont(new java.awt.Font("Dialog", 0, 12));
        UnselectCD.setText("Unselect");
        UnselectCD.setToolTipText("clears selection");
        UnselectCD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UnselectCDActionPerformed(evt);
            }
        });

        CDsPopup.add(UnselectCD);
        RemoveCD.setFont(new java.awt.Font("Dialog", 0, 12));
        RemoveCD.setText("Remove");
        RemoveCD.setToolTipText("removes selected CD's");
        RemoveCD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveCDActionPerformed(evt);
            }
        });

        CDsPopup.add(RemoveCD);
        AboutDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        AboutDialog.setTitle("About this program");
        AboutDialog.setModal(true);
        AboutLabel.setBackground(new java.awt.Color(50, 50, 200));
        AboutLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("AboutLogo.JPG")));
        AboutDialog.getContentPane().add(AboutLabel, java.awt.BorderLayout.CENTER);

        flagpanel.setBackground(new java.awt.Color(50, 50, 200));
        IndiaFlag.setBackground(new java.awt.Color(50, 50, 200));
        IndiaFlag.setIcon(new javax.swing.ImageIcon(getClass().getResource("indiaflag.gif")));
        flagpanel.add(IndiaFlag);

        AboutDialog.getContentPane().add(flagpanel, java.awt.BorderLayout.NORTH);

        // SearchDialog.getContentPane().setLayout(null);

        SearchDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        SearchDialog.setTitle("Search");
        SearchDialog.setResizable(false);
        slabel.setFont(new java.awt.Font("Dialog", 0, 12));
        slabel.setText("Search For");
        SearchDialog.getContentPane().add(slabel);
        slabel.setBounds(50, 30, 80, 16);

        SearchDialog.getContentPane().add(stextfield);
        stextfield.setBounds(140, 30, 140, 20);

        slabeltype.setFont(new java.awt.Font("Dialog", 0, 12));
        slabeltype.setText("This is a");
        SearchDialog.getContentPane().add(slabeltype);
        slabeltype.setBounds(50, 70, 80, 20);

        cdtype.setFont(new java.awt.Font("Dialog", 0, 12));
        cdtype.setSelected(true);
        cdtype.setText("CD");
        cdtype.setToolTipText("If selected, searches for a CD with above mentioned name");
        SearchDialogButtonGroup.add(cdtype);
        SearchDialog.getContentPane().add(cdtype);
        cdtype.setBounds(140, 70, 104, 24);

        filetype.setFont(new java.awt.Font("Dialog", 0, 12));
        filetype.setText("File");
        filetype.setToolTipText("If selected, searches for a file with above name");
        SearchDialogButtonGroup.add(filetype);
        SearchDialog.getContentPane().add(filetype);
        filetype.setBounds(140, 100, 60, 24);

        searchbutton.setFont(new java.awt.Font("Dialog", 0, 12));
        searchbutton.setText("Search");
        SearchDialog.getContentPane().add(searchbutton);
        searchbutton.setBounds(50, 140, 73, 26);

        closebutton.setFont(new java.awt.Font("Dialog", 0, 12));
        closebutton.setText("Close");
        SearchDialog.getContentPane().add(closebutton);
        closebutton.setBounds(160, 140, 67, 26);

        // SortDialog.getContentPane().setLayout(null);

        SortDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        SortDialog.setTitle("Sort");
        SortDialog.setModal(true);
        SortDialog.setResizable(false);
        SortButton.setFont(new java.awt.Font("Dialog", 0, 12));
        SortButton.setText("Sort");
        SortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SortButtonActionPerformed(evt);
            }
        });

        SortDialog.getContentPane().add(SortButton);
        SortButton.setBounds(40, 160, 75, 25);

        CloseButton.setFont(new java.awt.Font("Dialog", 0, 12));
        CloseButton.setText("Close");
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        SortDialog.getContentPane().add(CloseButton);
        CloseButton.setBounds(170, 160, 75, 25);

        SortByPanel.setLayout(new java.awt.GridLayout(4, 0));

        SortByPanel.setBorder(new javax.swing.border.EtchedBorder());
        SortByLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        SortByLabel.setText("Sort By");
        SortByLabel.setToolTipText("");
        SortByPanel.add(SortByLabel);

        LabelRadio.setFont(new java.awt.Font("Dialog", 0, 12));
        LabelRadio.setSelected(true);
        LabelRadio.setText("Label");
        SortDialogButtonGroup.add(LabelRadio);
        SortByPanel.add(LabelRadio);

        InfoRadio.setFont(new java.awt.Font("Dialog", 0, 12));
        InfoRadio.setText("Information");
        SortDialogButtonGroup.add(InfoRadio);
        SortByPanel.add(InfoRadio);

        LenderRadio.setFont(new java.awt.Font("Dialog", 0, 12));
        LenderRadio.setText("Lender");
        SortDialogButtonGroup.add(LenderRadio);
        SortByPanel.add(LenderRadio);

        SortDialog.getContentPane().add(SortByPanel);
        SortByPanel.setBounds(20, 20, 120, 120);

        OrderPanel.setLayout(new java.awt.GridLayout(4, 0));

        OrderPanel.setBorder(new javax.swing.border.EtchedBorder());
        OrderLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        OrderLabel.setText("Order");
        OrderPanel.add(OrderLabel);

        AscendingCheck.setFont(new java.awt.Font("Dialog", 0, 12));
        AscendingCheck.setSelected(true);
        AscendingCheck.setText("Ascending");
        SortOrderButtonGroup.add(AscendingCheck);
        OrderPanel.add(AscendingCheck);

        DescendingCheck.setFont(new java.awt.Font("Dialog", 0, 12));
        DescendingCheck.setText("Descending");
        SortOrderButtonGroup.add(DescendingCheck);
        OrderPanel.add(DescendingCheck);

        SortDialog.getContentPane().add(OrderPanel);
        OrderPanel.setBounds(150, 20, 110, 120);

        Label.setBackground(new java.awt.Color(50, 50, 200));
        Label.setFont(new java.awt.Font("Dialog", 0, 12));
        Label.setIcon(new javax.swing.ImageIcon(getClass().getResource("CDLibrary.jpg")));
        Label.setOpaque(true);
        Boolean b=new Boolean(properties.getProperty("ViewLogo"));
        boolean viewlogobool=b.booleanValue();
        if(viewlogobool)
        {
            getContentPane().add(Label,BorderLayout.SOUTH);
        }
        SplashscreenLabel.setBackground(new java.awt.Color(50, 50, 200));
        SplashscreenLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("AboutLogo.JPG")));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CD Library");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        tabbedpane.setToolTipText("Shows CD's in a specified way");
        tabbedpane.setFont(new java.awt.Font("Dialog", 0, 12));
        tabbedpane.setPreferredSize(new java.awt.Dimension(450, 400));
        CDTable.setModel(CDTable.getModel());
        CDTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                CDTableMouseReleased(evt);
            }
        });

        CDScrollPane.setViewportView(CDTable);

        tabbedpane.addTab("CD's", null, CDScrollPane, "CD tab");

        getContentPane().add(tabbedpane, java.awt.BorderLayout.CENTER);

        tmenu_file.setMnemonic('f');
        tmenu_file.setText("File");
        tmenu_file.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_new.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_new.setMnemonic('n');
        menu_new.setText("New");
        menu_new.setToolTipText("Displays New CD Dialog");
        menu_new.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_newActionPerformed(evt);
            }
        });

        tmenu_file.add(menu_new);
        menu_update.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_update.setMnemonic('u');
        menu_update.setText("Update");
        menu_update.setToolTipText("Updates the Data in the file");
        menu_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_updateActionPerformed(evt);
            }
        });

        tmenu_file.add(menu_update);
        menu_exit.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_exit.setMnemonic('x');
        menu_exit.setText("Exit");
        menu_exit.setToolTipText("quit from this application");
        menu_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_exitActionPerformed(evt);
            }
        });

        tmenu_file.add(menu_exit);
        menubar.add(tmenu_file);
        tmenu_edit.setMnemonic('e');
        tmenu_edit.setText("Edit");
        tmenu_edit.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_refresh.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        menu_refresh.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_refresh.setMnemonic('r');
        menu_refresh.setText("Refresh");
        menu_refresh.setToolTipText("Refreshes the CD's Table");
        menu_refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Refresh(evt);
            }
        });

        tmenu_edit.add(menu_refresh);
        menu_count.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_count.setMnemonic('c');
        menu_count.setText("Count");
        menu_count.setToolTipText("Counts the number of CD's");
        menu_count.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_countActionPerformed(evt);
            }
        });

        tmenu_edit.add(menu_count);
        tmenu_edit.add(separator);
        menu_sort.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_sort.setMnemonic('s');
        menu_sort.setText("Sort");
        menu_sort.setToolTipText("Displays sort Dialogbox");
        menu_sort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_sortActionPerformed(evt);
            }
        });

        tmenu_edit.add(menu_sort);
        menu_search.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        menu_search.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_search.setMnemonic('e');
        menu_search.setText("Search");
        menu_search.setToolTipText("Displays search Dialogbox");
        menu_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_searchActionPerformed(evt);
            }
        });

        tmenu_edit.add(menu_search);
        menubar.add(tmenu_edit);
        tmenu_view.setMnemonic('v');
        tmenu_view.setText("View");
        tmenu_view.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_viewlogo.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_viewlogo.setMnemonic('v');
        cmenu_viewlogo.setSelected(viewlogobool);
        cmenu_viewlogo.setText("Logo");
        cmenu_viewlogo.setToolTipText("shows CD Library logo at the bottom of the window");
        cmenu_viewlogo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmenu_viewlogoItemStateChanged(evt);
            }
        });

        tmenu_view.add(cmenu_viewlogo);
        tmenu_view.add(viewSeparator1);
        cmenu_OnlyLended.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_OnlyLended.setText("Only  Lended");
        cmenu_OnlyLended.setToolTipText("Checks whether only lended items to be shown or not");
        tmenu_view.add(cmenu_OnlyLended);
        menubar.add(tmenu_view);
        tmenu_themes.setMnemonic('T');
        tmenu_themes.setText("Themes");
        tmenu_themes.setToolTipText("Select a visual theme");
        tmenu_themes.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_metal.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_metal.setSelected(true);
        cmenu_metal.setText("Metal");
        cmenu_metal.setToolTipText("Select Metal look and feel");
        themesbuttongroup.add(cmenu_metal);
        cmenu_metal.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmenu_metalItemStateChanged(evt);
            }
        });

        tmenu_themes.add(cmenu_metal);
        cmenu_motif.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_motif.setText("Motif");
        cmenu_motif.setToolTipText("Select Motif look and feel");
        themesbuttongroup.add(cmenu_motif);
        cmenu_motif.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmenu_motifItemStateChanged(evt);
            }
        });

        tmenu_themes.add(cmenu_motif);
        cmenu_system.setFont(new java.awt.Font("Dialog", 0, 12));
        cmenu_system.setText("System");
        cmenu_system.setToolTipText("Select System look and feel");
        themesbuttongroup.add(cmenu_system);
        cmenu_system.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmenu_systemItemStateChanged(evt);
            }
        });

        tmenu_themes.add(cmenu_system);
        menubar.add(tmenu_themes);
        tmenu_about.setMnemonic('a');
        tmenu_about.setText("About");
        tmenu_about.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_about.setFont(new java.awt.Font("Dialog", 0, 12));
        menu_about.setMnemonic('a');
        menu_about.setText("About...");
        menu_about.setToolTipText("About this software and its developer");
        menu_about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_aboutActionPerformed(evt);
            }
        });

        tmenu_about.add(menu_about);
        menubar.add(tmenu_about);
        setJMenuBar(menubar);

        pack();
    }//GEN-END:initComponents

    private void cmenu_systemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmenu_systemItemStateChanged
        // Add your handling code here:
        if(cmenu_system.isSelected())
        {
            try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch(Exception e) { }
            SwingUtilities.updateComponentTreeUI(this);        
        }
    }//GEN-LAST:event_cmenu_systemItemStateChanged

    private void cmenu_motifItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmenu_motifItemStateChanged
        // Add your handling code here:
        if(cmenu_motif.isSelected())
        {
            try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            } catch(Exception e) { }
            SwingUtilities.updateComponentTreeUI(this);        
        }
    }//GEN-LAST:event_cmenu_motifItemStateChanged

    private void cmenu_metalItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmenu_metalItemStateChanged
        // Add your handling code here:
        if(cmenu_metal.isSelected())
        {
            try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch(Exception e) { }
            SwingUtilities.updateComponentTreeUI(this);
        }
    }//GEN-LAST:event_cmenu_metalItemStateChanged

    private void UnselectCDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UnselectCDActionPerformed
        // Add your handling code here:
        CDTable.clearSelection();
    }//GEN-LAST:event_UnselectCDActionPerformed

    private void menu_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_updateActionPerformed
        // Add your handling code here:
        try {
            FileOutputStream foutcd=new FileOutputStream("CD.jxt");
            ObjectOutputStream objoutcd=new ObjectOutputStream(foutcd);
            objoutcd.writeObject(Cds);
            objoutcd.close();
        }catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(null,"File Not Found");            
        }catch(IOException e){ 
            JOptionPane.showMessageDialog(null,"IOException Caught");
            e.printStackTrace();
        };                
    }//GEN-LAST:event_menu_updateActionPerformed

    private void menu_countActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_countActionPerformed
        // Add your handling code here:
        JOptionPane.showMessageDialog(mainframe,"Number of CD's are "+Cds.size());
    }//GEN-LAST:event_menu_countActionPerformed

    private void SortButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SortButtonActionPerformed
        // Add your handling code here:
        Vector temp=new Vector();
        boolean flag=false;
        if(AscendingCheck.isSelected())
            flag=true;
        else
            flag=false;
        try {
            if(LabelRadio.isSelected())
                temp.addAll(Sort(0,flag));
            else if(InfoRadio.isSelected())
                temp.addAll(Sort(1,flag));
            else if(LenderRadio.isSelected())
                temp.addAll(Sort(2,flag));
        } catch(Exception e) {
            e.printStackTrace();
        };
        Cds.removeAllElements();
        Cds.addAll(temp);        
        CDScrollPane.validate();
        CDScrollPane.repaint();
        ((JDialog)((JButton)evt.getSource()).getTopLevelAncestor()).dispose();        
    }//GEN-LAST:event_SortButtonActionPerformed

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        // Add your handling code here:
        SortDialog.dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void menu_sortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_sortActionPerformed
        SortDialog.setVisible(true);
    }//GEN-LAST:event_menu_sortActionPerformed

    private void menu_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_searchActionPerformed
        SearchDialog.setVisible(true);
    }//GEN-LAST:event_menu_searchActionPerformed

    private void cmenu_viewlogoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmenu_viewlogoItemStateChanged
        // Add your handling code here:
        if(((JCheckBoxMenuItem)evt.getSource()).isSelected())
        {
            mainframe.getContentPane().add(Label,BorderLayout.SOUTH);
            mainframe.validate();
            mainframe.repaint();
        }
        else
        {
            mainframe.getContentPane().remove(Label);
            mainframe.validate();
            mainframe.repaint();            
        }
    }//GEN-LAST:event_cmenu_viewlogoItemStateChanged

    private void CDTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CDTableMouseReleased
        // Add your handling code here:
        if(evt.isPopupTrigger())
            CDsPopup.show(evt.getComponent(),evt.getX(),evt.getY());
    }//GEN-LAST:event_CDTableMouseReleased

    private void RemoveCDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveCDActionPerformed
        // Add your handling code here:
        int x[]=CDTable.getSelectedRows();
        if(x.length==0) 
            JOptionPane.showMessageDialog(null,"no rows selected");        
        else
        {
            int choice=JOptionPane.showConfirmDialog(mainframe,
                                "Do you want to delete the selected "+CDTable.getSelectedRowCount()+ " CD's",
                                "Confirmation",JOptionPane.YES_NO_OPTION);
            if(choice!=JOptionPane.YES_OPTION)
                return;
            for(int i=0;i<x.length;i++)
            {
                // after every deletion an element number decrease
                Cds.removeElementAt(x[i]-i);
            }
            CDTable.clearSelection();
        }        
        CDTable.revalidate();        
    }//GEN-LAST:event_RemoveCDActionPerformed

    private void menu_aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_aboutActionPerformed
        // Add your handling code here:
        AboutDialog.setBounds((screensize.width/2)-200,(screensize.height/2)-175,400,350);
        AboutDialog.setVisible(true);
    }//GEN-LAST:event_menu_aboutActionPerformed

    private void menu_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_exitActionPerformed
        // Add your handling code here:
        OnExit();
    }//GEN-LAST:event_menu_exitActionPerformed

    private void Refresh(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Refresh
        // Add your handling code here:
        CDTable.revalidate();
    }//GEN-LAST:event_Refresh

    private void menu_newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_newActionPerformed
        // Add your handling code here:
        NewDialog nd=new NewDialog(this,true);
        nd.setSize(375,350);   
        nd.setVisible(true);
    }//GEN-LAST:event_menu_newActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        OnExit();
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        int width=500,height=600;
        
        properties=new Properties();
        try {
            FileInputStream fin=new FileInputStream("CDLibrary.properties");
            properties.load(fin);
            fin.close();
        } catch(FileNotFoundException e) {
            properties.setProperty("Width","500");
            properties.setProperty("Height","600");
            properties.setProperty("ViewLogo",Boolean.toString(true));
        } catch(IOException e) {
            JOptionPane.showMessageDialog(null,"Unable to load properties");
        }
        
        mainframe=new CDLibrary();
        
        width=Integer.parseInt(properties.getProperty("Width"));
        height=Integer.parseInt(properties.getProperty("Height"));
        
        mainframe.setSize(width,height);
        mainframe.setVisible(true);
    }
    void OnExit()
    {
        try {
            FileOutputStream fout=new FileOutputStream("CDLibrary.properties");

            properties.setProperty( "Width" , Integer.toString( mainframe.getWidth() ) );
            properties.setProperty( "Height" , Integer.toString( mainframe.getHeight() ) );
            properties.setProperty( "ViewLogo" , Boolean.toString( mainframe.cmenu_viewlogo.isSelected() ) );
            
            properties.store(fout,"--Properties--");
            fout.close();
        } catch(FileNotFoundException e) {
            JOptionPane.showMessageDialog(null,"properties file not found");
        } catch(IOException e) {
            JOptionPane.showMessageDialog(null,"Unable to save properties");
        }
        try {                       
            FileOutputStream foutcd=new FileOutputStream("CD.jxt");
            ObjectOutputStream objoutcd=new ObjectOutputStream(foutcd);
            objoutcd.writeObject(Cds);
            objoutcd.close();
        }catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(null,"Unable to save");            
        }catch(IOException e){ 
            JOptionPane.showMessageDialog(null,"IOException Caught");
            e.printStackTrace();
        };
        dispose();        
        System.exit(0);        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem menu_about;
    private javax.swing.JTabbedPane tabbedpane;
    private javax.swing.JCheckBoxMenuItem cmenu_system;
    private javax.swing.JButton SortButton;
    private javax.swing.JMenuBar menubar;
    private javax.swing.JMenu tmenu_view;
    private javax.swing.JMenu tmenu_file;
    private javax.swing.JButton searchbutton;
    private javax.swing.ButtonGroup SortDialogButtonGroup;
    private javax.swing.ButtonGroup themesbuttongroup;
    private javax.swing.JRadioButton filetype;
    private javax.swing.JScrollPane CDScrollPane;
    private javax.swing.JMenuItem menu_update;
    private javax.swing.JLabel SplashscreenLabel;
    private javax.swing.JCheckBoxMenuItem cmenu_metal;
    private javax.swing.JCheckBoxMenuItem cmenu_OnlyLended;
    private javax.swing.JLabel slabeltype;
    private javax.swing.JRadioButton cdtype;
    private javax.swing.JMenuItem menu_count;
    private javax.swing.JMenu tmenu_about;
    private javax.swing.JCheckBoxMenuItem cmenu_viewlogo;
    private javax.swing.JPanel OrderPanel;
    private javax.swing.JCheckBox DescendingCheck;
    private javax.swing.JPopupMenu CDsPopup;
    private javax.swing.JDialog SortDialog;
    private javax.swing.ButtonGroup SortOrderButtonGroup;
    private javax.swing.JMenu tmenu_themes;
    private javax.swing.JMenu tmenu_edit;
    private javax.swing.ButtonGroup SearchDialogButtonGroup;
    private javax.swing.JMenuItem menu_exit;
    private javax.swing.JMenuItem UnselectCD;
    private javax.swing.JRadioButton LabelRadio;
    private javax.swing.JLabel Label;
    private javax.swing.JLabel slabel;
    private javax.swing.JButton CloseButton;
    static javax.swing.JTable CDTable;
    private javax.swing.JMenuItem menu_sort;
    private javax.swing.JLabel SortByLabel;
    private javax.swing.JRadioButton LenderRadio;
    private javax.swing.JSeparator separator;
    private javax.swing.JMenuItem menu_new;
    private javax.swing.JLabel IndiaFlag;
    private javax.swing.JTextField stextfield;
    private javax.swing.JPanel SortByPanel;
    private javax.swing.JLabel AboutLabel;
    private javax.swing.JButton closebutton;
    private javax.swing.JMenuItem RemoveCD;
    private javax.swing.JDialog AboutDialog;
    private javax.swing.JMenuItem menu_refresh;
    private javax.swing.JRadioButton InfoRadio;
    private javax.swing.JCheckBoxMenuItem cmenu_motif;
    private javax.swing.JLabel OrderLabel;
    private javax.swing.JMenuItem menu_search;
    private javax.swing.JSeparator viewSeparator1;
    private javax.swing.JCheckBox AscendingCheck;
    private javax.swing.JDialog SearchDialog;
    private javax.swing.JPanel flagpanel;
    // End of variables declaration//GEN-END:variables
    
    Dimension screensize;
    public void VectorInitialisation()
    {
        Cds=new Vector();
        CdColumnNames=new Vector();                
        try {
            FileInputStream fpcd=new FileInputStream("CD.jxt");
            ObjectInputStream objcd=new ObjectInputStream(fpcd);
            Cds=(Vector)objcd.readObject();
            objcd.close();
        }catch(FileNotFoundException e) {
            JOptionPane.showMessageDialog(null,"File Not Found");
        }catch(IOException e){
            JOptionPane.showMessageDialog(null,"IOException caught");            
            e.printStackTrace();
        }catch(Exception e){ };
        
        CdColumnNames.addElement("CD");
        CdColumnNames.addElement("Info");
        CdColumnNames.addElement("Person Lended To");       
    }
    
    Vector Sort(int SortByItem,boolean order)
    {
        /* order means order of sorting
         *    true means ascending
         *    false means descending
         * SortByItem means the basis of sorting
         *    0 means By Label
         *    1 means By Information
         *    2 means By Lender
         */
        
        Vector temp,temp1,ReturnVector;
        String s1,s2;
        
        ReturnVector=new Vector();
        
        for(int i=0;i<Cds.size();i++)
        {
            temp=(Vector)Cds.get(i);            
            s1=(String)temp.get(SortByItem);
            if(s1==null)
            {
                s1=" ";
                temp.setElementAt(" ",2);
                Cds.setElementAt(temp,i);
            }            
            for(int j=0;j<Cds.size();j++)
            {
                if(j==i)
                    continue;
                temp1=(Vector)Cds.get(j);
                s2=(String)temp1.get(SortByItem);
                if(s2==null)
                {
                    s2=" ";
                    temp1.setElementAt(" ",2);
                    Cds.setElementAt(temp1,j);
                }
                if(order == true)
                {
                    if(s1.compareTo(s2)<0)                        
                    {
                        s1=s2;
                        temp=temp1;
                    }
                    else
                        continue;
                }
                else if(order == false)
                {
                    if(s1.compareTo(s2)<0)                        
                        continue;
                    else
                    {
                        s1=s2;
                        temp=temp1;                        
                    }
                }
            }
            
            ReturnVector.addElement(temp);
            Cds.removeElement(temp); 
            i=0;            
        }
        ReturnVector.addAll(Cds);
        return ReturnVector;
    }
}

class CdClass
{
    String CDLabel;
    String CDInfo;
    String CDTaker;
    public CdClass(String Cdl,String Cdi)
    {
        CDLabel=Cdl;
        CDInfo=Cdi;
        CDTaker=" ";
    }
    final Vector getVector()
    {
        Vector temp=new Vector();
        temp.addElement(CDLabel);
        temp.addElement(CDInfo);
        temp.addElement(CDTaker);
        return temp;
    }    
}
